package gui;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;

import Entities.AssessmentEntity;
import Entities.RequestEntity;

import common.ChatIF;
import common.CommonMethods;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import javax.swing.JScrollPane;
import javax.xml.datatype.Duration;
import java.awt.SystemColor;

public class Analyze extends JPanel implements ChatIF{
	String pid;
	String request;
	String deadline;
	AssessorPanel assessor;
	ViewRequestPanel viewRequest;
	Analyze analyze;
	AcademicFrame frame;
	AssessmentEntity assessorEntity;
	private JTextField textField;
	private JButton btnSend;
	private JTextArea constrictionstextArea;
	private JTextField locationtextField;
	private JTextArea changetextArea;
	private JTextArea benefittextArea;
	private JTextField timetextField;
	private JTextField deadlinetextField;
	private JButton btnExtendDuration;
	private RequestEntity myEnt;
	private String myStatus;
	private JTextArea reviewtextArea;
	
	public Analyze(AcademicFrame frame,AssessorPanel assessor, String pid,String request,String deadline, AssessmentEntity assessmentEntity) {
		super();
		this.pid=pid;
		this.request=request;
		this.deadline=deadline;
		this.frame=frame;
		this.assessor=assessor;	
		this.analyze=this;
		this.assessorEntity=assessmentEntity;	
		initialize();
	}
	
	private void initialize() {
		setLayout(null);
		
		JLabel lblAnalyzeRequest = new JLabel("Analyze Request");
		lblAnalyzeRequest.setHorizontalAlignment(SwingConstants.LEFT);
		lblAnalyzeRequest.setForeground(Color.BLACK);
		lblAnalyzeRequest.setFont(new Font("Tahoma", Font.BOLD, 32));
		lblAnalyzeRequest.setBounds(171, 11, 308, 56);
		add(lblAnalyzeRequest);
		
		JLabel lblRequest = new JLabel("Request#:");
		lblRequest.setBounds(57, 81, 60, 14);
		add(lblRequest);
		
		textField = new JTextField();
		textField.setEditable(false);
		textField.setBounds(112, 78, 86, 20);
		textField.setText(request);
		add(textField);
		textField.setColumns(10);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(137, 357, 368, 56);
		add(scrollPane_2);
		
		constrictionstextArea = new JTextArea();
		constrictionstextArea.setText(assessorEntity.getConstrictions());
		scrollPane_2.setViewportView(constrictionstextArea);
		constrictionstextArea.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				if(constrictionstextArea.getText().isEmpty() || changetextArea.getText().isEmpty() || benefittextArea.getText().isEmpty() || timetextField.getText().isEmpty() || locationtextField.getText().isEmpty()){
					btnSend.setEnabled(false);
				}
				else{
						btnSend.setEnabled(true);
				}
			}
		});
		
		JButton btnViewRequest = new JButton("View Request");
		btnViewRequest.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				viewRequest=new ViewRequestPanel(request, frame, analyze,false,"");

				frame.setSize(550, 730);	
				frame.setContentPane(viewRequest);
			}
		});
		btnViewRequest.setBounds(419, 78, 118, 23);
		add(btnViewRequest);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setSize(500, 500);
				frame.setContentPane(assessor);
			}
		});
		btnCancel.setBounds(78, 457, 120, 23);
		add(btnCancel);
		
		btnSend = new JButton("Send");
		btnSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				try{
					String duration= timetextField.getText();
					Integer time= Integer.parseInt(duration);
					ArrayList<Object> message= new ArrayList<Object>();
					String location=locationtextField.getText();
					String description=changetextArea.getText();					
					String benefit= benefittextArea.getText();
					String constrictions= constrictionstextArea.getText();
					String newStatus="5";
					AssessmentEntity assessmentEntity= new AssessmentEntity();
					assessmentEntity.setRid(request);
					assessmentEntity.setLocation(location);
					assessmentEntity.setDurationAssessment(duration);
					assessmentEntity.setChangeDescription(description);
					assessmentEntity.setBenefit(benefit);
					assessmentEntity.setConstrictions(constrictions);
					message.add("setAssessment");
					message.add(assessmentEntity);
					message.add(newStatus);
					System.out.println("Sent.");
					AcademicFrame.myclient.sendMessageToServer(message);	
				}
				catch(NumberFormatException ex){
					(new Dialog ("The 'Estimated time for execution' field excepts only numbers")).setVisible(true);
					timetextField.setText("");
				}
			}
		});
		btnSend.setEnabled(false);
		btnSend.setBounds(446, 457, 118, 23);
		add(btnSend);
		
		JLabel lblChangeDescription = new JLabel("Change Description:");
		lblChangeDescription.setBounds(10, 221, 133, 14);
		add(lblChangeDescription);
		
		JLabel lblBenefit = new JLabel("Benefit:");
		lblBenefit.setBounds(78, 289, 65, 14);
		add(lblBenefit);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(137, 222, 368, 56);
		add(scrollPane);
		
		changetextArea = new JTextArea();
		changetextArea.setText(assessorEntity.getChangeDescription());
		scrollPane.setViewportView(changetextArea);
		changetextArea.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				if(constrictionstextArea.getText().isEmpty() || changetextArea.getText().isEmpty() || benefittextArea.getText().isEmpty() || timetextField.getText().isEmpty() || locationtextField.getText().isEmpty()){
					btnSend.setEnabled(false);
				}
				else{
						btnSend.setEnabled(true);
				}
			}
		});
		
		JLabel lblConstrictions = new JLabel("Constrictions:");
		lblConstrictions.setBounds(52, 356, 105, 14);
		add(lblConstrictions);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(137, 290, 368, 56);
		add(scrollPane_1);
		
		benefittextArea = new JTextArea();
		benefittextArea.setText(assessorEntity.getBenefit());
		scrollPane_1.setViewportView(benefittextArea);
		benefittextArea.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				if(constrictionstextArea.getText().isEmpty() || changetextArea.getText().isEmpty() || benefittextArea.getText().isEmpty() || timetextField.getText().isEmpty() || locationtextField.getText().isEmpty()){
					btnSend.setEnabled(false);
				}
				else{
						btnSend.setEnabled(true);
				}
			}
		});
		
		JLabel lblSystem = new JLabel("Location:");
		lblSystem.setBounds(112, 140, 73, 14);
		add(lblSystem);
		
		locationtextField = new JTextField();
		locationtextField.setBounds(198, 137, 158, 20);
		locationtextField.setText(assessorEntity.getLocation());
		add(locationtextField);
		locationtextField.setColumns(10);
		
		JLabel lblEstimatedTimeFor = new JLabel("Estimated time for execution:");
		lblEstimatedTimeFor.setBounds(21, 112, 195, 14);
		add(lblEstimatedTimeFor);
		
		timetextField = new JTextField();
		timetextField.setBounds(198, 109, 158, 20);
		timetextField.setText(assessorEntity.getDurationAssessment());
		add(timetextField);
		timetextField.setColumns(10);
		
		btnExtendDuration = new JButton("Extend Duration");
		btnExtendDuration.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (CommonMethods.exceededDeadline(deadlinetextField.getText()))
				{	
					(new Dialog ("Deadline has passed, cannot extend.")).setVisible(true);
					btnExtendDuration.setEnabled(false);
				}
				else
					if (CommonMethods.moreThan3Days(deadlinetextField.getText()) )
					{
						(new Dialog ("More than 3 days to the deadline, cannot extend.")).setVisible(true);
						
						btnExtendDuration.setEnabled(false);
					}
					else
					{
						ExtendDeadline extendFrame = new ExtendDeadline(deadlinetextField.getText(), btnExtendDuration, request, "4", "assessment", "wasExtended", "Rid" );
						extendFrame.setVisible(true);
						extendFrame.setSize(760,400);
					}
			}
		});
		btnExtendDuration.setBounds(260, 457, 120, 23);
		add(btnExtendDuration);
		
		deadlinetextField = new JTextField();
		deadlinetextField.setText(deadline);
		deadlinetextField.setEditable(false);
		deadlinetextField.setColumns(10);
		deadlinetextField.setBounds(294, 78, 86, 20);
		add(deadlinetextField);
		
		JLabel label = new JLabel("Deadline:");
		label.setBounds(243, 81, 56, 14);
		add(label);
		
		JScrollPane scrollPane_3 = new JScrollPane();
		scrollPane_3.setBounds(137, 168, 368, 42);
		add(scrollPane_3);
		
		reviewtextArea = new JTextArea();
		reviewtextArea.setBackground(SystemColor.menu);
		reviewtextArea.setText(assessorEntity.getReviewed());
		scrollPane_3.setViewportView(reviewtextArea);
		reviewtextArea.setEditable(false);
		
		JLabel lblReview = new JLabel("Review:");
		lblReview.setBounds(71, 167, 72, 14);
		add(lblReview);
	}
	public void display(Object message)
	{	
		ArrayList<String> replyFromServer = (ArrayList<String>)message; 	
		ArrayList<Object> requestedData=new ArrayList<Object>();
		requestedData.clear();
		requestedData= (ArrayList<Object>)message;
		System.out.println((String)(replyFromServer.get(0)));
		if( ((String)(replyFromServer.get(0))).equalsIgnoreCase("setAssessment")){
			if ( ((String)(replyFromServer.get(1))).equalsIgnoreCase("AssessmentSet") )
			{
				System.out.println("done");
				(new Dialog ("Your assessment was sent, thank you.")).setVisible(true);	
				frame.setSize(500, 500);
				frame.setContentPane(assessor);	
			}	
			else
			{
				System.out.println("error");
				(new Dialog ("An Error occured while writing to the database.")).setVisible(true);
				frame.setSize(500, 500);
				frame.setContentPane(assessor);	
			}
		}
		else if (((String)(requestedData.get(0))).equalsIgnoreCase("requestData"))
		{
			myEnt = (RequestEntity)requestedData.get(1);
			myStatus=myEnt.getStatusid();
			myEnt.setStatus(myStatus);
			deadlinetextField.setText(myEnt.getDeadline());
		}
	}
}
